#!/bin/bash

#
# Installation of OpenJDK version 8.
# 
# Target platform: Based on Ubuntu(Ubuntu Kylin/Ubuntu Desktop/Linux Mint/ZorinOS/DeepinOS), include graphical interface, and not applicable to any server version.
# Create date: 2019/07/08
# Create user: SteveJrong
# File version: 1.0.0

# Import some shells.
. common/common.sh

# ######################### start of variables #########################
APP_PACKAGE_KEYWORD_1="openjdk"
APP_PACKAGE_KEYWORD_2="8u40"
APP_PACKAGE_SUFFIX="tar.gz"
APP_PACKAGE_ORIGINAL_NAME_DECOMPRESSED="java-se-8u40-ri"
APP_PACKAGE_NAME=$APP_PACKAGE_KEYWORD_1-$APP_PACKAGE_KEYWORD_2-linux-x64
INSTALL_DIRECTORY_NAME=$APP_PACKAGE_KEYWORD_1-$APP_PACKAGE_KEYWORD_2
APP_PACKAGE_DOWNLOAD_LINK="https://download.java.net/openjdk/jdk8u40/ri/jdk_ri-8u40-b25-linux-x64-10_feb_2015.tar.gz"
JAVA_HOME=`echo '${JAVA_HOME}'`
JRE_HOME=`echo '${JRE_HOME}'`
JRE_PATH=`echo '$PATH'`
PROFILE_CONTENT="export JAVA_HOME=$USER_PATH/$INSTALL_DIRECTORY_NAME/$APP_PACKAGE_ORIGINAL_NAME_DECOMPRESSED\n
export JRE_HOME=$JAVA_HOME/jre\n
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib\n
export PATH=.:$JAVA_HOME/bin:$JRE_PATH"
# ######################### end of variables #########################

# ######################### start of functions #########################
# ######################### end of functions #########################

# ######################### start of install OpenJDK version 8 #########################
echo '------------------------- Starting install OpenJDK version 8.'

echo '------------------------- Installation status checked.'
if [ -e "$USER_PATH/$INSTALL_DIRECTORY_NAME" ]; then
    # Installation directory already exists, need delete first.
    echo '------------------------- Installation directory already exists, need delete first.'
	sudo rm -rf $USER_PATH/$INSTALL_DIRECTORY_NAME
fi
sudo mkdir $USER_PATH/$INSTALL_DIRECTORY_NAME
echo -e "------------------------- Done.\n"
sleep 5

echo '------------------------- Checking temporary file path.'
if [ ! -d "$TEMP_FILE_PATH" ]; then
	# Temporary directory does not exist, need to create it.
	echo '------------------------- Temporary directory does not exist, create it first.'

	sudo mkdir -p $TEMP_FILE_PATH
	sudo chmod -R 777 $TEMP_FILE_PATH
else
	# Temporary directory already exists, need to check of old temporary files.
	checkOldTemporaryFiles=`find $TEMP_FILE_PATH -iname "*$APP_PACKAGE_NAME*.$APP_PACKAGE_SUFFIX"`
	if [ -f "$checkOldTemporaryFiles" ]; then
		echo '------------------------- Compression package file already exists, delete it first.'

		# Application package file already exists, need delete first.
		sudo rm -f $checkOldTemporaryFiles
	fi
fi
echo -e "------------------------- Done.\n"
sleep 5

echo '------------------------- Downloading compression package.'
wget -P $TEMP_FILE_PATH $APP_PACKAGE_DOWNLOAD_LINK -O $TEMP_FILE_PATH/$APP_PACKAGE_NAME.$APP_PACKAGE_SUFFIX
echo -e "------------------------- Done.\n"
sleep 5

echo '------------------------- Decompressing compression package.'
sudo tar -zxvf $TEMP_FILE_PATH/$APP_PACKAGE_NAME.$APP_PACKAGE_SUFFIX -C $USER_PATH/$INSTALL_DIRECTORY_NAME
echo -e "------------------------- Done.\n"
sleep 5

echo '------------------------- Backup profile.'
sudo chmod 777 /etc/profile
sudo cp /etc/profile /etc/profile.bak
echo -e '------------------------- Done.\n'
sleep 5

echo '------------------------- Edit profile.'
echo -e $PROFILE_CONTENT >>/etc/profile
echo -e '------------------------- Done.\n'
sleep 5

echo '------------------------- Update profile.'
source /etc/profile
echo -e '------------------------- Done.\n'

outputSuccessMsg "Install OpenJDK version 8 is complete!"
# ######################### end of install OpenJDK version 8 #########################